package com.quanquan.parking.ketop.controller;

import DTO.PageDTO;
import com.quanquan.parking.ketop.dto.KetopParkingRecordDTO;
import com.quanquan.parking.ketop.dto.PaymentRechargeDTO;
import com.quanquan.parking.ketop.dto.UserDisCountDTO;
import com.quanquan.parking.ketop.entity.ParkingRecord;
import com.quanquan.parking.ketop.entity.UserDisCount;
import com.quanquan.parking.ketop.entity.params.request.PayParkingFeeRequest;
import com.quanquan.parking.ketop.entity.params.request.UserCarKetopRequest;
import com.quanquan.parking.ketop.request.UserDisCountRequest;
import com.quanquan.parking.ketop.service.KetopParkingService;
import com.quanquan.parking.ketop.service.UserDisCountService;
import com.quanquan.parking.request.PayNotifyRequest;
import enums.ResCodeEnum;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import utils.StringUtil;
import web.response.ResultBody;

import java.util.List;
import java.util.Map;

/**
 * @ClassName:ParkingController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/4/2 11:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/ketop_parking")
public class KetopParkingController {

    private final KetopParkingService ketopParkingService;
    private final UserDisCountService userDisCountService;

    @Autowired
    public KetopParkingController(KetopParkingService ketopParkingService, UserDisCountService userDisCountService) {
        this.ketopParkingService = ketopParkingService;
        this.userDisCountService = userDisCountService;
    }


    /**
     * 解绑车牌
     *
     * @return
     */
    @PostMapping("/delete_user_car")
    public ResultBody deleteUserCar(@RequestBody UserCarKetopRequest request) {
        if (StringUtil.isEmpty(request.getPlateNum()) && StringUtil.isEmpty(request.getUserId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        boolean flag = ketopParkingService.deleteUserCar(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "车牌解绑成功");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
    }

    /**
     * 用户绑定的车牌列表
     *
     * @return
     */
    @PostMapping("/find_by_user_id")
    public ResultBody findByUserId(@RequestBody UserCarKetopRequest request) {
        if (StringUtil.isEmpty(request.getUserId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        List<Object> objectList = ketopParkingService.findByUserId(request);
        if (objectList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无数据");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), objectList);
    }

    /**
     * 查询车费
     *
     * @return
     */
    @PostMapping("/query_parking_fee")
    public ResultBody queryParkingFee(@RequestBody UserCarKetopRequest request) {
        if (StringUtil.isEmpty(request.getUserId()) && StringUtil.isEmpty(request.getPlateNum())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        List<Object> objectList = ketopParkingService.queryParkingFee(request);
        if (objectList == null || objectList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无车辆停车信息");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), objectList);
    }


    /**
     * 查询车费，生成订单
     *
     * @param userCarKetopRequest
     * @return
     */
    @PostMapping("/query_fee_and_create_order")
    public ResultBody queryParkingFeeAndCreateOrder(@RequestBody UserCarKetopRequest userCarKetopRequest) {
        if (StringUtil.isEmpty(userCarKetopRequest.getPlateNum()) || StringUtil.isEmpty(userCarKetopRequest.getUserId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        List<ParkingRecord> parkingRecords = ketopParkingService.queryParkingFeeAndCreateOrder(userCarKetopRequest);
        if (parkingRecords == null || parkingRecords.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());
        }
        KetopParkingRecordDTO ketopParkingRecordDTO = new KetopParkingRecordDTO();
        BeanUtils.copyProperties(parkingRecords.get(0), ketopParkingRecordDTO);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), ketopParkingRecordDTO);
    }

    /**
     * 更新停车账单（当费用为0的时候调用）
     *
     * @return
     * @see [类、类#方法、类#成员]
     */
    @PostMapping("/update_order")
    public ResultBody updateOrder(@RequestBody PayParkingFeeRequest request) {
        if (StringUtil.isEmpty(request.getOrderNo())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        boolean b = ketopParkingService.updateOrder(request);
        if (b) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());
        }
        return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());
    }

    /**
     * 更新停车账单（扣减积分失败的时候调用）
     *
     * @return
     * @see [类、类#方法、类#成员]
     */
    @PostMapping("/update_order_fail")
    public ResultBody updateOrderFail(@RequestBody PayParkingFeeRequest request) {
        if (StringUtil.isEmpty(request.getOrderNo())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        boolean b = ketopParkingService.updateOrderFail(request);
        if (b) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());
        }
        return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());
    }


    /**
     * 同步停车数据（科拓、本地的停车数据）
     *
     * @return
     */
    @PostMapping("/pay_parking_fee")
    public ResultBody payParkingFee(@RequestBody PayParkingFeeRequest request) {
        /*if (StringUtil.isEmpty(request.getOrderNo())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }*/
        ParkingRecord parkingRecord = ketopParkingService.payParkingFee(request);
        if (parkingRecord != null) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
    }

    /**
     * 先调起科拓的抵扣，同步停车数据（科拓、本地的停车数据）
     *
     * @return
     */
    @PostMapping("/get_payment_recharge")
    public ResultBody getPaymentRecharge(@RequestBody PayParkingFeeRequest request) {
        /*if (StringUtil.isEmpty(request.getOrderNo())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }*/
        PaymentRechargeDTO paymentRecharge = ketopParkingService.getPaymentRecharge(request);
        if (paymentRecharge != null) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), paymentRecharge);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
    }

    /**
     * 查找停车订单
     *
     * @return
     */
    @PostMapping("/query_by_parking_order_num")
    public ResultBody queryByParkingOrderNum(@RequestBody PayParkingFeeRequest request) {
        List<ParkingRecord> parkingRecords = ketopParkingService.queryByParkingOrderNum(request);
        if (parkingRecords.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());
        }
        KetopParkingRecordDTO ketopParkingRecordDTO = new KetopParkingRecordDTO();
        BeanUtils.copyProperties(parkingRecords.get(0), ketopParkingRecordDTO);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), ketopParkingRecordDTO);
    }


    /**
     * 停车记录
     *
     * @return
     */
    @PostMapping("/query_ketop_parking_record")
    public ResultBody queryKetopParkingRecord(@RequestBody UserCarKetopRequest request) {
        List<ParkingRecord> parkingRecords = ketopParkingService.findByUserIdAndStatus(request);
        if (parkingRecords.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), parkingRecords);
    }

    /**
     * 停车记录（后台）
     *
     * @param request
     * @return
     */
    @PostMapping("/parking_record")
    public ResultBody parkingRecord(@RequestBody PayNotifyRequest request) {
        PageDTO<KetopParkingRecordDTO> payNotifyDTOPageDTO = ketopParkingService.parkingRecord(request);
        if (null == payNotifyDTOPageDTO || payNotifyDTOPageDTO.getList().size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", payNotifyDTOPageDTO);
    }

    /**
     * 查找会员权益
     *
     * @return
     * @see [类、类#方法、类#成员]
     */
    @PostMapping("/user_discount_front")
    public ResultBody userDisCountFront(@RequestBody PayParkingFeeRequest request) {
        UserDisCount userDisCount = userDisCountService.userDisCountFront();
        if (userDisCount == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), userDisCount);
    }

    /**
     * 使用会员权益
     *
     * @return
     * @see [类、类#方法、类#成员]
     */
    @PostMapping("/use_user_discount")
    public ResultBody useUserDiscount(@RequestBody PayParkingFeeRequest request) {
        Map<String, Object> map = ketopParkingService.useUserDiscount(request);
        if (map != null && map.containsKey("msg") && map.get("msg").equals("SUCCESS")) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), map);
        } else {
            return new ResultBody(ResCodeEnum.CUSTOM.getCode(), (String) map.get("msg"));
        }
    }

    /**
     * 确认使用会员权益
     *
     * @return
     * @see [类、类#方法、类#成员]
     */
    @PostMapping("/sure_use_user_discount")
    public ResultBody sureUseUserDiscount(@RequestBody PayParkingFeeRequest request) {
        String s = ketopParkingService.sureUseUserDiscount(request);
        if (s.equals("SUCCESS")) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), s);
    }

    /**
     * 使用会员权益
     *
     * @return
     * @see [类、类#方法、类#成员]
     */
    @PostMapping("/use_integral")
    public ResultBody useIntegral(@RequestBody PayParkingFeeRequest request) {
        if (request.getIntegral() == null || request.getIntegral() <= 0 || request.getUserId() == null) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        Map<String, Object> map = ketopParkingService.useIntegral(request);
        if (map != null && map.containsKey("msg") && map.get("msg").equals("SUCCESS")) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), map);
        } else {
            return new ResultBody(ResCodeEnum.CUSTOM.getCode(), (String) map.get("msg"));
        }
    }

    /**
     * 确认使用会员权益
     *
     * @return
     * @see [类、类#方法、类#成员]
     */
    @PostMapping("/sure_use_integral")
    public ResultBody sureUseIntegral(@RequestBody PayParkingFeeRequest request) {
        if (request.getIntegral() == null || request.getIntegral() <= 0 || request.getUserId() == null) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        String s = ketopParkingService.sureUseIntegral(request);
        if (s.equals("SUCCESS")) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), s);
    }


    /**
     * 查找会员权益(后台)
     *
     * @return
     * @see [类、类#方法、类#成员]
     */
    @PostMapping("/user_discount_back")
    public ResultBody userDisCountBack(@RequestBody PayParkingFeeRequest request) {
        UserDisCount userDisCount = userDisCountService.userDisCountBack();

        if (userDisCount == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());
        }
        UserDisCountDTO userDisCountDTO = new UserDisCountDTO();
        BeanUtils.copyProperties(userDisCount, userDisCountDTO);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), userDisCount);
    }

    /**
     * 更新会员权益
     *
     * @return
     * @see [类、类#方法、类#成员]
     */
    @PostMapping("/update_user_discount")
    public ResultBody updateUserDisCount(@RequestBody UserDisCountRequest request) {
        boolean b = userDisCountService.updateDisCountBack(request);
        if (b) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
    }

    /**
     * 同步停车数据（科拓、本地的停车数据）
     *
     * @return
     */
    @PostMapping("/pay_parking_fee_old")
    public ResultBody payParkingFeeOld(@RequestBody PayParkingFeeRequest request) {
        if (StringUtil.isEmpty(request.getOrderNo())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        boolean flag = ketopParkingService.payParkingFeeOld(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
    }

    /**
     * 先调起科拓的抵扣，同步停车数据（科拓、本地的停车数据）
     *
     * @return
     */
    @PostMapping("/get_payment_recharge_old")
    public ResultBody getPaymentRechargeOld(@RequestBody PayParkingFeeRequest request) {
        if (StringUtil.isEmpty(request.getOrderNo())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        boolean flag = ketopParkingService.getPaymentRechargeOld(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
    }

    /**
     * 查询车费，生成订单
     *
     * @param userCarKetopRequest
     * @return
     */
    @PostMapping("/query_fee_and_create_order_old")
    public ResultBody queryParkingFeeAndCreateOrderOld(@RequestBody UserCarKetopRequest userCarKetopRequest) {
        if (StringUtil.isEmpty(userCarKetopRequest.getPlateNum()) || StringUtil.isEmpty(userCarKetopRequest.getUserId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        List<ParkingRecord> parkingRecords = ketopParkingService.queryParkingFeeAndCreateOrderOld(userCarKetopRequest);
        if (parkingRecords == null || parkingRecords.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());
        }
        KetopParkingRecordDTO ketopParkingRecordDTO = new KetopParkingRecordDTO();
        BeanUtils.copyProperties(parkingRecords.get(0), ketopParkingRecordDTO);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), ketopParkingRecordDTO);
    }


}
